package One;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class UDP {
    public static void main(String[] args) throws IOException {
        try(DatagramSocket socket = new DatagramSocket(9999);){
            byte[] buf = new byte[256];
            DatagramPacket packet = new DatagramPacket(buf, buf.length);
            while (true) {
                socket.receive(packet);
                InetAddress address = packet.getAddress();
                int port = packet.getPort();
                String data = new String(packet.getData()).trim();
                System.out.println("receive data from "+address+" at port "+port);
                System.out.println("data="+data);
                if(data.trim().equalsIgnoreCase("quit"))
                    break;
            }
        }
    }
}